import React from 'react'
import { Button, Form, Input,message } from "antd";
import { UserOutlined, LockOutlined } from "@ant-design/icons";
import { Link,useNavigate } from "react-router-dom";
import {RegisterApi} from '../request/api'
import "./less/Register.less";
export default function Register() {
  const navigate=useNavigate()
  const onFinish = (values) => {
    // console.log("Success:", values);
    RegisterApi({
      username:values.username,
      Password:values.Password
    }).then(res=>{
      console.log(res);
      if(res.errCode===0){
        message.success('注册成功马上跳转到登陆页');
        setTimeout(() => {
          navigate('/login')
        }, 2000);
        
      }else{
        message.error(res.message);
      }
    })
  };

  const onFinishFailed = (errorInfo) => {
    console.log("Failed:", errorInfo);
  };
  return (
  

<div className="register">
      <div className="register_box">
        <Form
          name="basic"
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="Username"
            name="username"
            wrapperCol={{
              offset: 3,
              span: 16,
            }}
            rules={[
              {
                required: true,
                message: "Please input your username!",
              },
            ]}
          >
            <Input
              prefix={<UserOutlined />}
              placeholder="Please input your username!"
            />
          </Form.Item>

          <Form.Item
           wrapperCol={{
            offset: 3,
            span: 16,
          }}
            label="Password"
            name="password"
            rules={[
              {
                required: true,
                message: "Please input your password!",
              },
            ]}
          >
            <Input.Password
              prefix={<LockOutlined />}
              placeholder="Please input your password!"
            />
          </Form.Item>
          <Form.Item
        name="confirm"
        label="Confirm Password"
        dependencies={['password']}
        hasFeedback
        rules={[
          {
            required: true,
            message: 'Please confirm your password!',
          },
          ({ getFieldValue }) => ({
            validator(_, value) {
              if (!value || getFieldValue('password') === value) {
                return Promise.resolve();
              }

              return Promise.reject(new Error('The two passwords that you entered do not match!'));
            },
          }),
        ]}
      >
        <Input.Password />
      </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 2,
              span: 16,
            }}
          >
            <Link to="/login">
              I have account, to Login!
            </Link>
          </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 8,
              span: 16,
            }}
          >
            <Button type="primary" htmlType="submit">
              Register now！
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>


  )
}
